/**
 * 
 */
package control;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @author andy
 *
 */
public class catchNovelMainThread {
	private String html;
	private int nThread;
	private CountDownLatch startGate;
	private CountDownLatch endGate;
	
	 /**
	 * @param html
	 * @param nThread
	 * @param startGate
	 * @param endGate
	 */
	public catchNovelMainThread(String html, int nThread, CountDownLatch startGate, CountDownLatch endGate) {
		this.html = html;
		this.nThread = nThread;
		this.startGate = startGate;
		this.endGate = endGate;
	}

	public void timeTasks() {
		ArrayList<Thread> threadname = new ArrayList<Thread>();
		
	        for(int i = 0; i < nThread; i++){
	            Thread thread = new Thread(new catchNovelThread(html, startGate, endGate));
	            threadname.add(thread);
	            thread.start();
	        }

	        long start = System.currentTimeMillis();
	        //所有阻塞的任务同时开始
	        startGate.countDown();

	        try {
	            //主线程阻塞,等待其他所有线程完成后再执行
	            endGate.await();
//		        for (Thread string : threadname) {
//					System.out.println(string.getName() + ":" + string.isAlive());
//				}
	        } catch (InterruptedException e) {
	            e.printStackTrace();
	        }
	        long end = System.currentTimeMillis();
	        System.out.println("用时: " + (end - start) + "ms");

	    }

}
